/**
 * @date 4:16:38 PM
 * EducationAdminServiceImpl.java
 * Administrator
 * TODO
 */
package com.qs.education.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qs.education.bean.beans.EducationAdminResponse;
import com.qs.education.bean.beans.EducationTeacherResponse;
import com.qs.education.bean.entity.EducationAdmin;
import com.qs.education.bean.entity.EducationTeacher;
import com.qs.education.bean.service.BaseService;
import com.qs.education.bean.utils.PasswordUtils;
import com.qs.education.service.IAdminAPIService;

/**
 * @class EducationAdminServiceImpl
 * @author Brooks
 * @description 
 * @date Oct 17, 2018
 */
@Service
public class AdminAPIServiceImpl extends BaseService implements IAdminAPIService{

	/**
	 * @description 根据用户名和密码得到当前管理员
	 * @user Brooks
	 * @date Oct 17, 2018
	 * @return EducationAdminBean
	 */
	public EducationAdminResponse getEducationAdmin(String name, String password) {
		String md5Pass = PasswordUtils.encodPassword(password);
		EducationAdmin educationAdmin = iEducationAdminDao.getEducationAdmin(name, md5Pass);
		EducationAdminResponse educationAdminBean = new EducationAdminResponse();
		if(educationAdmin != null){
			BeanUtils.copyProperties(educationAdmin, educationAdminBean);
		}
		return educationAdminBean;
	}

	@Override
	public EducationAdminResponse findAdminByName(String name) {
		EducationAdmin admin = iEducationAdminDao.findAdminByName(name);
		if(null != admin){
			EducationAdminResponse educationAdminResponse = new EducationAdminResponse();
			BeanUtils.copyProperties(admin, educationAdminResponse);
			return educationAdminResponse;
		}
		return null;
	}

	@Override
	@Transactional
	public EducationAdminResponse updateAdmin(String name, String oldPassword, String newPassword) {
		
		EducationAdminResponse educationAdminBean = new EducationAdminResponse();
		String md5Pass = PasswordUtils.encodPassword(oldPassword);
		EducationAdmin educationAdmin = iEducationAdminDao.getEducationAdmin(name, md5Pass);
		if (educationAdmin!=null) {
			educationAdmin.setPassword(PasswordUtils.encodPassword(newPassword));
			iEducationAdminDao.save(educationAdmin);
			BeanUtils.copyProperties(educationAdmin, educationAdminBean);
		}
		
		return educationAdminBean;
	}


}
